package com.bloveoacom.http;

import android.widget.TextView;

import com.bloveoacom.exception.ApiException;

import org.reactivestreams.Publisher;

import java.util.concurrent.TimeUnit;

import io.reactivex.BackpressureStrategy;
import io.reactivex.Flowable;
import io.reactivex.FlowableEmitter;
import io.reactivex.FlowableOnSubscribe;
import io.reactivex.FlowableTransformer;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * author: yjl
 * e-mail: 1271901673@qq.com
 * time  : 2018/9/7
 * desc  :
 */
public class RxHelper {
    public static <T> FlowableTransformer<T,T> io_main(){
        return  new FlowableTransformer<T, T>() {
            @Override
            public Publisher<T> apply(Flowable<T> upstream) {
                return upstream.observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io());
            }
        };
    }

    public static <T> FlowableTransformer<HttpResult<T>,T>  getData(){
        return  new FlowableTransformer<HttpResult<T>, T>() {
            @Override
            public Publisher<T> apply(Flowable<HttpResult<T>> upstream) {
                return  upstream .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread()).flatMap(new Function<HttpResult<T>, Publisher<T>>() {
                    @Override
                    public Publisher<T> apply(HttpResult<T> tHttpResult) throws Exception {
                        if (tHttpResult.isOk()) {
                            T t = tHttpResult.isEmpty() ? ((T) "") : tHttpResult.data;
                            return createData(t);
                        } else {
                            return Flowable.error(new ApiException(tHttpResult.code, tHttpResult.msg));
                        }
                    }
                });
            }
        };
    }

    public static <T> Flowable<T> createData(final T t){
        return  Flowable.create(new FlowableOnSubscribe<T>() {
            @Override
            public void subscribe(FlowableEmitter<T> e) throws Exception {
                if (!e.isCancelled()) {
                    try {
                        e.onNext(t);
                        e.onComplete();
                    } catch (Exception e1) {
                        e1.printStackTrace();
                        e.onError(ApiException.handleException(e1));
                    }
                }
            }
        }, BackpressureStrategy.BUFFER);
    }



    public static int count = 60;

    public static void getCode(final TextView tv){
        Observable.interval(0, 1,TimeUnit.SECONDS).take(count+1).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(Disposable d) {
                tv.setEnabled(false);
            }

            @Override
            public void onNext(Long aLong) {
                tv.setText(count-aLong +"s");
            }

            @Override
            public void onError(Throwable e) {
                tv.setEnabled(true);
                tv.setText("重新获取");
            }

            @Override
            public void onComplete() {
                tv.setEnabled(true);
                tv.setText("重新获取");
            }
        });


    }






}
